package client;

import com.alibaba.fastjson.JSON;
import entity.Params;
import entity.PushCallback;
import entity.Sound;
import entity.Urls;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
/**
 * Title:Server 这
 * Description: 服务器向多个是发送消息的服务端客户端推送主题，即不同客户端可向服务器订阅相同主题
 *
 * @author Unclue_liu
 **/

/**
 * MQTT服务端
 * @program: hw
 * @description: qiuheyi
 * @author: qhy
 * @create: 2020-08-18 11:26
 **/
public class ServerMQTT {

    //tcp://MQTT安装的服务器地址:MQTT定义的端口号
    private String HOST;
    //定义一个主题
    private String TOPIC;
    //定义MQTT的ID，可以在MQTT服务配置中指定
    private String clientid;

    private MqttClient client;

    private static MqttTopic topic11;

    private String userName;
    private String passWord;

    private static MqttMessage message;

    /**
     * 构造函数
     *
     * @throws MqttException
     */
    public ServerMQTT() throws MqttException {
        // MemoryPersistence设置clientid的保存形式，默认为以内存保存
        client = new MqttClient(HOST, clientid, new MemoryPersistence());
        connect();
    }

    /**
     * 用来连接服务器
     */
    private void connect() {
        MqttConnectOptions options = new MqttConnectOptions();
        options.setCleanSession(false);
        options.setUserName(userName);
        options.setPassword(passWord.toCharArray());
        // 设置超时时间
        options.setConnectionTimeout(10);
        // 设置会话心跳时间
        options.setKeepAliveInterval(20);
        try {
            client.setCallback(new PushCallback());
            client.connect(options);
            topic11 = client.getTopic(TOPIC);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param topic
     * @param message
     * @throws MqttPersistenceException
     * @throws MqttException
     */
    public static void publish(MqttTopic topic, MqttMessage message) throws MqttPersistenceException,
            MqttException {
        MqttDeliveryToken token = topic.publish(message);

        token.waitForCompletion();
        System.out.println("消息已发布! " + token.isComplete());
    }


    public static void sendMessage(String msg) throws MqttException {
        ServerMQTT server = new ServerMQTT();
        message = new MqttMessage();
        //保证消息能到达一次
        message.setQos(0);
        message.setRetained(true);
//        String str ="{\"clieId\":\""+clieId+"\",\"mag\":\""+msg+"\"}";
        message.setPayload(msg.getBytes());
        try {
            publish(topic11, message);
            //断开连接
//            server.client.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    /**
     * 启动入口
     *
     * @param args
     * @throws MqttException
     */
    public static void main(String[] args) throws MqttException {
//        Sound sound = new Sound();
//        sound.setType("req");
//        sound.setCallid(1166556110L);
//        sound.setApp("ls20");
//        sound.setOp("login");
//
//        Params params = new Params();
////        params.setType(4);
//        params.setName("admin");
//        params.setPassword("admin");
//        params.setReplace(0L);
//        params.setSource("test_mqtt_client_111");
////        params.setUid("5a8e2454-69dd-4501-9955-7e59baaf59ce");
////        params.setVol(74);
////        List<String> devices = new ArrayList<>();
////        devices.add("02:03:DE:03:94:A0");
////        devices.add("02:02:7E:0E:29:90");
////        params.setDevices(devices);
////        params.setTid("757102c4-1ec6-4ce0-9b55-b395bd6903");
////        params.setUid("5a8e2454-69dd-4501-9955-7e59baaf59ce");
//
////
////        Urls urls = new Urls();
////        urls.setName("报警");
////        urls.setPause(0);
////        urls.setUri("/Users/qiuheyi/Downloads/死了都要爱.mp3");
//
//        sound.setParams(params);
////        sound.setUrls(urls);
//        String msg = JSON.toJSONString(sound);
//        sendMessage(msg);
    }

}
